package com.nan.hrm.config.controller;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MQHandler {
    public static final String EXCHANGE_NAME_TOPIC="exchange_name_topic";
    public static final String QUEUE_NAME_EMAIL="queue_name_email";
    public static final String QUEUE_NAME_SMS="queue_name_sms";
    public static final String QUEUE_NAME_SYSTEM="queue_name_system";
    //发送邮件
    @RabbitListener(queues = QUEUE_NAME_EMAIL,containerFactory = "rabbitListenerContainerFactory"
    )
    public void emailHandler(String courseName, Message msg , Channel channel){
        try {
            //手动签收
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
            System.out.println("邮件发送："+courseName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //发送短信
    @RabbitListener(queues = QUEUE_NAME_SMS,containerFactory = "rabbitListenerContainerFactory"
    )
    public void smsHandler(String courseName, Message msg , Channel channel){
        try {
            //手动签收
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
            System.out.println("短信发送："+courseName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //发送站内信
    @RabbitListener(queues = QUEUE_NAME_SYSTEM,containerFactory = "rabbitListenerContainerFactory"
    )
    public void systemMessageHandler(String courseName, Message msg , Channel channel){
        try {
            //手动签收
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
            System.out.println("系统发送："+courseName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
